<?php

// $Id$


require_once(TANGRA_MAIN_DIR.'interfaces/i_db_storable.class.php');


require_once('tcms_image.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/tcms_ci_image_dbc.class.php');


class TCMS_Image_DBC extends TCMS_Image implements I_DB_Storable {
	public function save(DB_Connection $dbc) {
		$ret = false;

		if ($this->get_id()) {
			$ret = $this->update($dbc);
		} else {
			$ret = $this->insert($dbc);
			$this->set_id($ret);
		}

		return $ret;
	}


	public function load_by_id(DB_Connection $dbc, $id) {
		tangra_if_not_int_throw_e($id);

		$ret = false;

		$sql = "select orig_filename, "
						."size, "
						."size_x, "
						."size_y, "
						."path, "
						."filename, "
						."note, "
						."alt "
					."from tcms_images where id = $id";

		$rez = $dbc->execute($sql);

		if (!$rez->is_eof()) {
			$rez_obj = $rez->fetch_object();

			$this->set_id($id);
			$this->set_orig_filename(stripslashes($rez_obj->ORIG_FILENAME));
			$this->set_size($rez_obj->SIZE);
			$this->set_size_x($rez_obj->SIZE_X);
			$this->set_size_y($rez_obj->SIZE_Y);
			$this->set_path(stripslashes($rez_obj->PATH));
			$this->set_filename(stripslashes($rez_obj->FILENAME));
			$this->set_note(stripslashes($rez_obj->NOTE));
			$this->set_alt(stripslashes($rez_obj->ALT));

			$ret = $id;
		}

		return $ret;
	}


	protected function insert(DB_Connection $dbc) {
		$ret = false;

		$id = $dbc->generate_id('tcms_images_seq');

		$orig_filename = addslashes($this->get_orig_filename());
		$size = $this->get_size();
		$size_x = $this->get_size_x();
		$size_y = $this->get_size_y();
		$path = addslashes($this->get_path());
		$filename = addslashes($this->get_filename());
		$note = addslashes($this->get_note());
		$alt = addslashes($this->get_alt());


		if ($id) {
			$sql = "insert into tcms_images (id, ".
									      "orig_filename, ".
									      "size, ".
									      "size_x, ".
									      "size_y, ".
									      "path, ".
									      "filename, ".
									      "note, ".
									      "alt ".
									      ") ".
								"values ".
									      "($id,".
									      "'$orig_filename', ".
									      "$size, ".
									      "$size_x, ".
									      "$size_y, ".
									      "'$path', ".
									      "'$filename', ".
									      "'$note', ".
									      "'$alt' ".
									      ")";
			$dbc->execute($sql);
			$ret = $id;
		} else {
			throw new TE_Exception('tcms_images_seq');
		}

		return $ret;
	}


	protected function update(DB_Connection $dbc) {
		$id = $this->get_id();

		$orig_filename = addslashes($this->get_orig_filename());
		$size = $this->get_size();
		$size_x = $this->get_size_x();
		$size_y = $this->get_size_y();
		$path = addslashes($this->get_path());
		$filename = addslashes($this->get_filename());
		$note = addslashes($this->get_note());
		$alt = addslashes($this->get_alt());

		$sql = "update tcms_images set ".
						"orig_filename = '$orig_filename', ".
						"size = $size, ".
						"size_x = $size_x, ".
						"size_y = $size_y, ".
						"path = '$path', ".
						"filename = '$filename', ".
						"note = '$note', ".
						"alt = '$alt' ".
					"where id = ".$id;

		$dbc->execute($sql);
		$ret = $id;

		return $ret;
	}


	public static function delete(DB_Connection $dbc, $id) {
		tangra_if_not_int_throw_e($id);

		$sql = "delete from tcms_images where id = $id";
		$dbc->execute($sql);
	}


	public static function delete_cascade(DB_Connection $dbc, $id) {
		TCMS_Image_DBC::delete($dbc, $id);
		TCMS_CI_Image_DBC::delete_for_image($dbc, $id);
	}


	public static function get_sql_for_grid($filename, $notealt) {
		$where = self::prepare_where_clause($filename, $notealt);
		$sql = "select id, orig_filename, size, size_x, size_y, filename, note, alt from tcms_images i ".$where." order by id desc";

		return $sql;
	}


	public static function get_sql_count_for_grid($filename, $notealt) {
		$where = self::prepare_where_clause($filename, $notealt);
		$sql = "select count(id) as total_rows from tcms_images i ".$where;

		return $sql;
	}


	private static function prepare_where_clause($filename, $notealt) {
		$filename = self::remove_duplicate_asteriks($filename);
		$notealt = self::remove_duplicate_asteriks($notealt);

		if ($filename || $notealt) {
			$where = '';
			if ($filename) {
				if (strpos($filename, '*') !== false) {
					$filename = addslashes($filename);
					$filename = str_replace('*', '%', $filename);
					$where .= "i.orig_filename like '$filename' ";
				} else {
					$filename = addslashes($filename);
					$where .= "i.orig_filename = '$filename' ";
				}
			}

			if ($notealt) {
				if (strpos($notealt, '*') !== false) {
					$notealt = addslashes($notealt);
					$notealt = str_replace('*', '%', $notealt);
					if ($where) {
						$where .= "and ";
					}
					$where .= "(i.note like '$notealt' or i.alt like '$notealt')";
				} else {
					if ($where) {
						$where .= "and ";
					}
					$notealt = addslashes($notealt);
					$where .= "(i.note = '$notealt' or i.alt = '$notealt')";
				}
			}
		} else {
			$where = '';
		}

		if ($where) {
			$where = " where ".$where;
		}

		return $where;
	}


	private static function remove_duplicate_asteriks($filter) {
		$ret = $filter;
		while(strpos($ret, '**') !== false) {
			$ret = str_replace('**', '*', $ret);
		}

		return $ret;
	}

}